What is timestring?
The timestring npm package is a utility for parsing human-readable time strings into milliseconds. It allows users to convert strings like '2 days', '3 hours', or '5 minutes' into a numerical representation of time in milliseconds, which can be useful for scheduling, time calculations, and more.
What are timestring's main functionalities?
Parse time strings to milliseconds
This feature allows you to convert a human-readable time string into milliseconds. For example, '2 days' is converted to 172800000 milliseconds.
const timestring = require('timestring');
const milliseconds = timestring('2 days'); // returns 172800000
Support for various time units
The package supports parsing of various time units such as seconds, minutes, hours, days, weeks, months, and years. It can handle combinations of these units in a single string.
const timestring = require('timestring');
const milliseconds = timestring('3 hours 15 minutes'); // returns 11700000
Other packages similar to timestring
ms
The ms package is a utility for converting time strings to milliseconds and vice versa. It is similar to timestring in that it can parse strings like '2 days' or '3h' into milliseconds. However, ms also provides functionality to convert milliseconds back into a human-readable format, which timestring does not offer.
parse-duration
The parse-duration package is another tool for parsing human-readable time strings into milliseconds. It offers similar functionality to timestring but includes additional features such as custom time unit definitions and more flexible parsing options.
timestring
Parse a human readable time string into a time based value.
Installation
npm install --save timestring
Usage
Overview
const timestring = require('timestring')
let str = '1h 15m'
let time = timestring(str)
console.log(time)
By default the returned time value from timestring
will be in seconds.
The time string can contain as many time groups as needed:
const timestring = require('timestring')
let str = '1d 3h 25m 18s'
let time = timestring(str)
console.log(time)
and can be as messy as you like:
const timestring = require('timestring')
let str = '1 d 3HOurS 25 min 1 8s'
let time = timestring(str)
console.log(time)
Keywords
timestring
will parse the following keywords into time values:
ms, milli, millisecond, milliseconds
- will parse to millisecondss, sec, secs, second, seconds
- will parse to secondsm, min, mins, minute, minutes
- will parse to minutesh, hr, hrs, hour, hours
- will parse to hoursd, day, days
- will parse to daysw, week, weeks
- will parse to weeksmon, mth, mths, month, months
- will parse to monthsy, yr, yrs, year, years
- will parse to years
Keywords can be used interchangeably:
const timestring = require('timestring')
let str = '1day 15h 20minutes 15s'
let time = timestring(str)
console.log(time)
Return Time Value
By default the return time value will be in seconds. This can be changed by passing one of the following strings as an argument to timestring
:
ms
- Millisecondss
- Secondsm
- Minutesh
- Hoursd
- Daysw
- Weeksmth
- Monthsy
- Years
const timestring = require('timestring')
let str = '22h 16m'
let hours = timestring(str, 'h')
let days = timestring(str, 'd')
let weeks = timestring(str, 'w')
console.log(hours)
console.log(days)
console.log(weeks)
Optional Configuration
A few assumptions are made by default:
- There are 24 hours per day
- There are 7 days per week
- There are 4 weeks per month
- There are 12 months per year
- There are 365.25 days per year
These options can be changed by passing an options object as an argument to timestring
.
The following options are configurable:
hoursPerDay
daysPerWeek
weeksPerMonth
monthsPerYear
daysPerYear
const timestring = require('timestring')
let str = '1d'
let opts = {
hoursPerDay: 1
}
let time = timestring(str, 'h', opts)
console.log(time)
In the example above hoursPerDay
is being set to 1
. When the time string is being parsed, the return value is being specified as hours. Normally 1d
would parse to 24
hours (as by default there are 24 hours in a day) but because hoursPerDay
has been set to 1
, 1d
will now only parse to 1
hour.
This would be useful for specific application needs.
Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type 1d
i want 7.5 hours to be tracked. When they type 1w
i want 5 days to be tracked etc.
const timestring = require('timestring')
let opts = {
hoursPerDay: 7.5,
daysPerWeek: 5
}
let hoursToday = timestring('1d', 'h', opts)
let daysThisWeek = timestring('1w', 'd', opts)
console.log(hoursToday)
console.log(daysThisWeek)
It is important to note that the daysPerYear
configuration option will be used to convert a month or year to seconds, so if you are using custom configuration options make sure that you adjust this value to suit if you expect to be parsing timestrings containing months or years.
Notes
If the string that is passed into timestring
can not be parsed then an error will be thrown:
const timestring = require('timestring')
let str = 'aaabbbccc'
let time = timestring(str)
If a number is passed into timestring
it will be treated as a string containing milliseconds:
const timestring = require('timestring')
let time = timestring(3000)